from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.utils import timezone
from django.db.models.functions import ExtractYear, ExtractMonth
from django.db.models import Count

from api.subject_management.models import Subject
from api.experiment_management.models import Experiment, ExperimentalProgram, ExperimentProcedure, Riddle
from api.property_management.models import Sample, MacroOrganization, MicroStructure, ChemicalProperties, \
    PhysicalProperties, TensileMechanicalProperties, CompressingMechanicalProperties, ShearProperties, \
    BondProperties, TorsionalProperties, FractureToughness, FrictionalWearProperties, Hardness, ServiceProperties


def count_by_month(model, key):
    """
    按月统计
    """
    one_year_data = model.objects.filter(add_date__year=timezone.now().year)
    # count_result = one_year_data.annotate(year=ExtractYear('add_date'), month=ExtractMonth('add_date')) \
    #     .values( 'year', 'month').order_by('year', 'month').annotate(count=Count('chemical_properties_id'))
    return one_year_data.annotate(month=ExtractMonth('add_date')) \
        .values('month').order_by('month') \
        .annotate(count=Count(key))


@api_view(['GET'])
def get_growth_data_count(request):
    """
    获取录入数据量
    """
    result = {
        'subject': [], 'experiment': [], 'experimentalProgram': [], 'experimentProcedure': [],
        'riddle': [], 'sample': [], 'macroOrganization': [], 'microStructure': [],
        'chemicalProperties': [], 'physicalProperties': [], 'tensileMechanicalProperties': [],
        'compressingMechanicalProperties': [], 'shearProperties': [], 'bondProperties': [],
        'torsionalProperties': [], 'fractureToughness': [], 'frictionalWearProperties': [],
        'hardness': [], 'serviceProperties': []
    }
    result_index_list = [
        'subject', 'experiment', 'experimentalProgram', 'experimentProcedure', 'riddle', 'sample',
        'macroOrganization', 'microStructure', 'chemicalProperties', 'physicalProperties',
        'tensileMechanicalProperties', 'compressingMechanicalProperties', 'shearProperties',
        'bondProperties', 'torsionalProperties', 'fractureToughness', 'frictionalWearProperties',
        'hardness', 'serviceProperties'
    ]
    model_list = [
        Subject, Experiment, ExperimentalProgram, ExperimentProcedure, Riddle, Sample,
        MacroOrganization, MicroStructure, ChemicalProperties, PhysicalProperties,
        TensileMechanicalProperties, CompressingMechanicalProperties, ShearProperties,
        BondProperties, TorsionalProperties, FractureToughness, FrictionalWearProperties,
        Hardness, ServiceProperties
    ]
    key_list = [
        'subject_id', 'experiment_id', 'experiment_program_id', 'experiment_procedure_id', 'riddle_id',
        'sample_id', 'macro_organization_id', 'micro_structure_id', 'chemical_properties_id',
        'physical_properties', 'tensile_mechanical_properties_id', 'compress_mechanical_properties_id',
        'shear_properties_id', 'bond_properties_id', 'torsional_properties_id', 'fracture_toughness_id',
        'frictional_wear_properties_id', 'hardness_id', 'service_properties_id'
    ]
    for model, key, result_index in zip(model_list, key_list, result_index_list):
        result[result_index] = count_by_month(model, key)
    return Response(result, status=status.HTTP_200_OK)
    # return Response({
    #     "January": 1,
    #     "February": 2,
    #     "March": 3,
    #     "April": 4,
    #     "May": 5,
    #     "June": 6,
    #     "July": 7,
    #     "August": 8,
    #     "September": 9,
    #     "October": 10,
    #     "November": 11,
    #     "December": 12,
    # }, status=status.HTTP_200_OK)
